package com.msc.mobilecomputing.utils;

import android.content.res.AssetManager;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class FontUtils {
	private static Typeface normal;

	private static Typeface bold;

	private static Typeface condensed;

	private static Typeface light;

	private static Typeface swiss;

	private static void processsViewGroup(ViewGroup v, final int len) {

		for (int i = 0; i < len; i++) {
			final View c = v.getChildAt(i);
			if (c instanceof TextView) {
				setCustomFont((TextView) c);
			} else if (c instanceof Button) {
				setCustomFont((Button) c);
			} else if (c instanceof ViewGroup) {
				setCustomFont((ViewGroup) c);
			}
		}
	}

	private static void setCustomFont(TextView c) {
		Object tag = c.getTag();
		if (tag == null) {
			c.setTypeface(normal);
		} else {
			if (tag instanceof String) {
				if (((String) tag).equals("bold")
						|| (c.getTypeface() != null && c.getTypeface()
								.getStyle() == Typeface.BOLD)) {
					// c.setTypeface( bold, Typeface.BOLD );
					c.setTypeface(bold);
					return;
				}
				if (((String) tag).equals("condensed")) {
					// c.setTypeface( condensed, Typeface.NORMAL );
					c.setTypeface(condensed);
					return;
				}
				if (((String) tag).equals("light")) {
					// c.setTypeface( light, Typeface.NORMAL );
					c.setTypeface(light);
					return;
				}
				if (((String) tag).contains("swiss")) {
					if (((String) tag).contains("bold")) {
						c.setTypeface(swiss, Typeface.BOLD);
					} else {
						c.setTypeface(swiss);
					}
					return;
				}
			}
		}
		c.setTypeface(normal);
	}

	private static void setCustomFont(Button c) {
		Object tag = c.getTag();

		if (tag == null) {
			c.setTypeface(normal);
		} else {
			if (tag instanceof String) {
				if (((String) tag).equals("bold")
						|| c.getTypeface().getStyle() == Typeface.BOLD) {
					c.setTypeface(bold);
					// c.setTypeface( bold, Typeface.BOLD );
					return;
				}
				if (((String) tag).equals("condensed")) {
					c.setTypeface(condensed);
					// c.setTypeface( condensed );
					return;
				}
				if (((String) tag).equals("light")) {
					// c.setTypeface( light );
					c.setTypeface(light);
					return;
				}
				if (((String) tag).contains("swiss")) {
					if (((String) tag).contains("bold"))// swiss font with bold
					{
						c.setTypeface(swiss, Typeface.BOLD);
					} else {
						c.setTypeface(swiss);
					}
					return;
				}
			}
		}
		c.setTypeface(normal);
	}

	public static void setCustomFont(View topView, AssetManager assetsManager) {
		if (normal == null || bold == null || condensed == null
				|| light == null) {
			normal = Typeface.createFromAsset(assetsManager,
					"fonts/Roboto-Light.ttf");
			bold = Typeface.createFromAsset(assetsManager,
					"fonts/Roboto-Bold.ttf");
			condensed = Typeface.createFromAsset(assetsManager,
					"fonts/RobotoCondensed-Regular.ttf");
			light = Typeface.createFromAsset(assetsManager,
					"fonts/Roboto-Light.ttf");
			swiss = Typeface.createFromAsset(assetsManager,
					"fonts/Roboto-Light.ttf");

			// normal = Typeface.createFromAsset( assetsManager,
			// "fonts/swiss.ttf" );
			// bold = Typeface.createFromAsset( assetsManager, "fonts/swiss.ttf"
			// );
			// condensed = Typeface.createFromAsset( assetsManager,
			// "fonts/swiss.ttf" );
			// light = Typeface.createFromAsset( assetsManager,
			// "fonts/swiss.ttf" );
		}

		if (topView instanceof ViewGroup) {
			setCustomFont((ViewGroup) topView);
		} else if (topView instanceof TextView) {
			setCustomFont((TextView) topView);
		} else if (topView instanceof Button) {
			setCustomFont((TextView) topView);
		}
	}

	private static void setCustomFont(ViewGroup v) {
		final int len = v.getChildCount();
		processsViewGroup(v, len);
	}

	public static Typeface getNormal() {
		return normal;
	}

	public static Typeface getBold() {
		return bold;
	}

	public static Typeface getCondensed() {
		return condensed;
	}

	public static Typeface getLight() {
		return light;
	}

	public static Typeface getSwiss() {
		return swiss;
	}

}